package com.yx.frame.builder;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.yx.frame.builder.validate.ServiceBuilderConfigManager;
import com.yx.frame.builder.xml.MethodParmXml;
import com.yx.frame.builder.xml.UpdateMethodContentXml;
import com.yx.frame.builder.xml.UpdateMethodContentXml.UpdateSql;

import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;

public class UpdateContent extends Composite {
	private Shell window;
	private Table sqlTable;
	private String[] loopByParms;
	private Combo dataSrc;
	private Button userDefDataSrc;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public UpdateContent(Composite parent, int style, Shell win) {
		super(parent, style);
		this.window = win;

		Button button = new Button(this, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ViewUpdateSqlTxt ViewSqlTxt = new ViewUpdateSqlTxt(window,
						SWT.NONE, sqlTable, -1);
				ViewSqlTxt.open();
			}
		});
		button.setBounds(441, 8, 46, 27);
		button.setText("添加");

		Button button_2 = new Button(this, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int idx = sqlTable.getSelectionIndex();
				if (idx >= 0) {
					sqlTable.remove(idx);
					for (int i = 0; i < sqlTable.getItemCount(); i++) {
						sqlTable.getItem(i).setText(0, "" + (i + 1));
					}
				}
			}
		});
		button_2.setText("删除");
		button_2.setBounds(590, 8, 46, 27);

		sqlTable = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		sqlTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				int idx = sqlTable.getSelectionIndex();
				if (idx >= 0 && idx < sqlTable.getItemCount()) {
					ViewUpdateSqlTxt ViewSqlTxt = new ViewUpdateSqlTxt(window,
							SWT.NONE, sqlTable, idx);
					ViewSqlTxt.open();
				}
			}
		});
		sqlTable.setBounds(10, 37, 626, 341);
		sqlTable.setHeaderVisible(true);
		sqlTable.setLinesVisible(true);
		TableColumn tc1 = new TableColumn(sqlTable, SWT.CENTER);
		TableColumn tc2 = new TableColumn(sqlTable, SWT.LEFT);
		tc1.setText("序号");
		tc2.setText("数据库更新语句");
		tc1.setWidth(60);
		tc2.setWidth(530);

		Label label_1 = new Label(this, SWT.NONE);
		label_1.setBounds(10, 13, 46, 17);
		label_1.setText("数据源：");

		dataSrc = new Combo(this, SWT.NONE);
		dataSrc.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				String[] dbArray = ServiceBuilderConfigManager.getInstance()
						.getDataSource();
				String dTxt = dataSrc.getText();
				if (dbArray == null) {
					dataSrc.removeAll();
				} else {
					dataSrc.setItems(dbArray);
				}
				dataSrc.setText(dTxt);
			}
		});
		dataSrc.setBounds(62, 9, 137, 25);
		
		userDefDataSrc = new Button(this, SWT.CHECK);
		userDefDataSrc.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(userDefDataSrc.getSelection()){
					dataSrc.setText("");
					dataSrc.setEnabled(false);
				}else{
					dataSrc.setEnabled(true);
				}
			}
		});
		userDefDataSrc.setBounds(205, 13, 141, 17);
		userDefDataSrc.setText("用户访问时指定数据源");
		
		Button button_1 = new Button(this, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int selectIdx=sqlTable.getSelectionIndex();
				if(sqlTable.getItemCount()<1)return;
				if(selectIdx<0||selectIdx>=sqlTable.getItemCount()-1){
					return ;
				}
				TableItem selectItem=sqlTable.getItem(selectIdx);
				TableItem nextItem=sqlTable.getItem(selectIdx+1);
				String tmp;
				for(int i=0;i<2;i++){
					tmp=selectItem.getText(i);
					selectItem.setText(i, nextItem.getText(i));
					nextItem.setText(i, tmp);
				}
				for (int i = 0; i < sqlTable.getItemCount(); i++) {
					sqlTable.getItem(i).setText(0, "" + (i + 1));
				}
				sqlTable.setSelection(selectIdx+1);
			}
		});
		button_1.setText("下移");
		button_1.setBounds(491, 8, 46, 27);
		
		Button button_3 = new Button(this, SWT.NONE);
		button_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int selectIdx=sqlTable.getSelectionIndex();
				if(sqlTable.getItemCount()<1)return;
				if(selectIdx<1||selectIdx>=sqlTable.getItemCount()){
					return ;
				}
				TableItem selectItem=sqlTable.getItem(selectIdx);
				TableItem upItem=sqlTable.getItem(selectIdx-1);
				String tmp;
				for(int i=0;i<2;i++){
					tmp=selectItem.getText(i);
					selectItem.setText(i, upItem.getText(i));
					upItem.setText(i, tmp);
				}
				for (int i = 0; i < sqlTable.getItemCount(); i++) {
					sqlTable.getItem(i).setText(0, "" + (i + 1));
				}
				sqlTable.setSelection(selectIdx-1);
			}
		});
		button_3.setText("上移");
		button_3.setBounds(541, 8, 46, 27);
	}

	public void setLoopbyListParms(List<String> listTypeParms) {
		loopByParms = listTypeParms.toArray(new String[listTypeParms.size()]);
	}

	public void setUpdateMethodContentXml(
			UpdateMethodContentXml updateMethodContentXml) {
		List<UpdateSql> list = updateMethodContentXml.getTransaction();
		for (int i = 0; i < list.size(); i++) {
			UpdateSql uSql = list.get(i);
			TableItem item1 = new TableItem(sqlTable, SWT.NONE);
			item1.setText(new String[] { "" + uSql.getIndex(), uSql.getSql() });
		}
		for (int i = 0; i < sqlTable.getItemCount(); i++) {
			sqlTable.getItem(i).setText(0, "" + (i + 1));
		}
		//callbackMethod.setText(updateMethodContentXml.getCallback());
		dataSrc.setText(updateMethodContentXml.getDatasource());
		String userDefDataStr=updateMethodContentXml.getUserDefineDatasource();
		if("true".equals(userDefDataStr)){
			userDefDataSrc.setSelection(true);
			dataSrc.setText("");
			dataSrc.setEnabled(false);			
		}else{
			userDefDataSrc.setSelection(false);
		}
	}

	public UpdateMethodContentXml getUpdateMethodContentXml(
			List<MethodParmXml> MethodParms) throws Exception {
		UpdateMethodContentXml updateMethodContentXml = new UpdateMethodContentXml();
		for (int i = 0; i < sqlTable.getItemCount(); i++) {
			TableItem item = sqlTable.getItem(i);
			String sqlId = item.getText(0);
			boolean isAllArray = true;
			boolean isExistArray = false;
			String loopParmName = "";
			for (int pIdx = 0; pIdx < MethodParms.size(); pIdx++) {
				MethodParmXml methodParmXml = MethodParms.get(pIdx);
				if (methodParmXml.getSqlIndex().equals(sqlId)) {
					if (methodParmXml.isMutilistType()) {
						isExistArray = true;
						loopParmName = methodParmXml.getName();
					} else {
						isAllArray = false;
					}
				}
			}
			if (isExistArray && isAllArray) {
				updateMethodContentXml.addUpdateSql(
						Integer.parseInt(item.getText(0)), item.getText(1),
						loopParmName);
			} else if (!isExistArray) {
				updateMethodContentXml.addUpdateSql(
						Integer.parseInt(item.getText(0)), item.getText(1), "");
			} else {
				throw new Exception("第"+sqlId+"条SQL的参数必须同时为数组或同时不是数组");
			}
		}
		updateMethodContentXml.setCallback("");
		updateMethodContentXml.setDatasource(dataSrc.getText().trim());
		if(userDefDataSrc.getSelection()){
			updateMethodContentXml.setUserDefineDatasource("true");
		}else{
			updateMethodContentXml.setUserDefineDatasource("false");
		}
		return updateMethodContentXml;
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
